class Queue {
  constructor(func){
    this.queue = [];
    this.execFunc = func;
    this.isRunning = false;
  }

  add(arr){
    this.queue.push(...arr);
    if(!this.isRunning){
      this.isRunning = true;
      this.cost();
    }
  }

  cost(){
   const current = this.queue.shift();
   if(!current){
     this.doneFunc?.();
     return this.isRunning = false;
   }
   this.execFunc(current, ()=>{
    this.cost();
   }, !this.queue.length);
  }

}

export default Queue;